<template>
  <div class="permissions_dialog">
    <el-dialog
      :title="isEDit ? '修改菜单' : '添加菜单'"
      center
      :visible.sync="dialogFormVisible"
      :before-close="handleClose"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <el-form :model="form" :rules="rules" ref="ruleForm">
        <el-form-item label="菜单名称" prop="title">
          <el-input
            v-model.trim="form.title"
            autocomplete="off"
            placeholder="请输入"
          ></el-input>
        </el-form-item>
        <el-form-item label="路由" prop="route">
          <el-input
            v-model.trim="form.route"
            autocomplete="off"
            placeholder="请输入"
          ></el-input>
        </el-form-item>
        <el-form-item label="权限ID" prop="permissions_id">
          <el-select
            v-model="form.permissions_id"
            filterable
            remote
            reserve-keyword
            placeholder="请输入权限关键词"
            :remote-method="remoteMethod"
            :loading="permissions_loading"
          >
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="父目录ID" prop="parent_id">
          <el-input
            v-model.trim="form.parent_id"
            autocomplete="off"
            placeholder="可选，不填为一级菜单"
          ></el-input>
        </el-form-item>
        <el-form-item label="是否侧边栏显示" prop="navigation">
          <el-switch
            v-model="form.navigation"
            :active-value="1"
            :inactive-value="0"
            active-text="显示"
            inactive-text="隐藏"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleClose">返回</el-button>
        <el-button
          :loading="btnLoading"
          type="primary"
          @click="submitForm('ruleForm')"
          v-no-more-click:3000
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { debounce } from "@u";
import { ApimenuAdd, ApimenuEdit, Apipermission } from "@a";
export default {
  data() {
    return {
      options: [],
      permissions_loading: false,
      isEDit: false,
      id: "",
      btnLoading: false,
      dialogFormVisible: false,
      form: {
        title: "",
        parent_id: "",
        route: "",
        permissions_id: "",
        navigation: 1,
      },

      rules: {
        title: [
          {
            required: true,
            message: "请输入",
            trigger: ["blur", "change"],
          },
        ],
        route: [
          {
            required: true,
            message: "请输入",
            trigger: ["blur", "change"],
          },
        ],
        permissions_id: [
          {
            required: true,
            message: "请输入",
            trigger: "blur",
          },
        ],
      },
    };
  },

  methods: {
    remoteMethod(query = "") {
      query = query.trim();
      if (query !== "") {
        this.permissions_loading = true;
        Apipermission({ name: query }).then((res) => {
          this.options = res.data || [];
          this.$nextTick((_) => {
            this.permissions_loading = false;
          });
        });
      } else {
        this.options = [];
      }
    },
    showDialog(data) {
      if (data) {
        this.isEDit = true;
        for (const key in this.form) {
          this.form[key] = data[key];
        }
        this.id = data.id;
      } else {
        this.isEDit = false;
      }
      this.dialogFormVisible = true;
    },

    // 提交数据
    submitForm: debounce(
      function (formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.btnLoading = true;
            const form = { ...this.form };
            if (!form.parent_id) form.parent_id = 0;

            if (this.isEDit) {
              this.editData(form);
            } else {
              this.addData(form);
            }
          } else {
            return false;
          }
        });
      },
      300,
      true
    ),

    addData(parmas) {
      ApimenuAdd(parmas)
        .then((res) => {
          this.changeData();
        })
        .finally((_) => {
          this.btnLoading = false;
        });
    },
    editData(parmas) {
      ApimenuEdit(parmas, this.id)
        .then((res) => {
          this.changeData();
        })
        .finally((_) => {
          this.btnLoading = false;
        });
    },
    changeData() {
      this.$message.success("操作成功！");
      this.$emit("changeList");
      this.handleClose();
    },
    handleClose() {
      this.resetForm("ruleForm");
    },
    resetForm(formName) {
      this.dialogFormVisible = false;
      this.$refs[formName].resetFields();
    },
  },
};
</script>
<style lang="less" scoped></style>
